<?php
/**
 * Images controller. Resizes and displays images on the fly. Saves resized images to cache.
 * 
 * @author Konstantin Shamko <konstantin.shamko@gmail.com> 
 * @version 0.0.1
 * @copyright  Copyright (c) 2009 Konstantin Shamko
 * @category VaselinEngine
 * @package Content Module
 * @subpackage Controller
 * @license  New BSD License
 *
 */
class content_ImagesController extends Zend_Controller_Action {
	
	/**
	 * Resises and displays images. Saves resized image to cache.
	 * Keeps image's proportions 
	 *
	 * @example <img src='/content/images/resized/?src=SRC&w=WIDTH&h=HEIGHT' /> 
	 */
	public function resizedAction() {
		$request = $this->_request->getParams ();
		
		$width = (isset ( $request ['w'] )) ? $request ['w'] : null;
		$height = (isset ( $request ['h'] )) ? $request ['h'] : null;
		$src = $request ['src'];
		
		$image_cache_dir = HOME_DIR . 'tmp/image_cache/';
		$_filename = $width . '_' . $height . '_' . basename ( $src );

		$im_pr = new Bel_File_Image();
		if (! file_exists ( $image_cache_dir . $_filename )) {
			$im_pr->open ( HOME_DIR . $src ); //$im_pr->resizeImage($src,array(array('width'=>$width,'height'=>$height)));
			$im_pr->setKeepProportion ( true )->resize ( $width, $height );
			$im_pr->save($image_cache_dir.$_filename);
			$im_pr->display ();
		} else {
			$im_pr->open($image_cache_dir . $_filename);
			$im_pr->display();
		}
		exit ();
	}
}